package com.am.lottery.view.list

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.rememberCursorPositionProvider
import cafe.adriel.voyager.core.screen.Screen
import com.am.lottery.service.loadHistoryData
import com.am.lottery.view.common.LoadingDialog
import com.am.lottery.view.common.LoadingState
import com.am.lottery.view.common.rememberMutableDialogState
import kotlinx.coroutines.launch
import org.slf4j.Logger
import org.slf4j.LoggerFactory

private val logger: Logger = LoggerFactory.getLogger(ListView::class.java)

object ListView : Screen {
    private fun readResolve(): Any = ListView

    @Preview
    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    override fun Content() {
        // Create a mutable dialog state
        val dialogState = rememberMutableDialogState<String?>(null)
        val scope = rememberCoroutineScope()

        // Display the LoadingDialog if the dialog is visible
        if (dialogState.isVisible.value) {
            LoadingDialog(dialogState.dialogData.value) // Pass the message to the dialog
        }
        
        Row {
            Column {
                Row {
                    TooltipBox(
                        positionProvider = rememberCursorPositionProvider(),
                        tooltip = { Text("刷新或初始化") },
                        state = TooltipState()
                    ) {
                        Button(
                            onClick = {
                                scope.launch {
                                    loadHistoryData().collect { state ->
                                        // Perform actions based on the received LoadingState
                                        when (state) {
                                            // Show the dialog with the provided message
                                            is LoadingState.Loading -> dialogState.showDialog(state.msg)
                                            // Hide the dialog
                                            is LoadingState.Idle -> dialogState.hideDialog()
                                        }
                                    }
                                }
                            },
                        ) {
                            Text(text = "加载历史数据")
                        }

                    }

                }
            }

        }
        Row {
            HorizontalDivider(
                modifier = Modifier.padding(top = 10.dp, bottom = 10.dp),
            )
        }
        Row {
            Column {
                ListItem(
                    headlineContent = { Text("Two line list item with trailing") },
                    supportingContent = { Text("Secondary text") },
                    trailingContent = { Text("meta") },
                    leadingContent = {
                        Icon(
                            Icons.Filled.Favorite,
                            contentDescription = "Localized description",
                        )
                    }
                )
                HorizontalDivider()
            }
        }
    }
}